package org.chs.ts.base.common.util;

import lombok.extern.slf4j.Slf4j;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

/**
 * @author wuwei
 * @date 2024/11/30
 * @apiNote
 */
@Slf4j
public class DateUtils {

    public static final ThreadLocal<SimpleDateFormat> YYYYMMDD = ThreadLocal.withInitial(()->new SimpleDateFormat(
            "yyyyMMdd"));
    public static final ThreadLocal<SimpleDateFormat> HHMMSS_ONE = ThreadLocal.withInitial(()->new SimpleDateFormat(
            "HH:mm:ss"));
    public static final ThreadLocal<SimpleDateFormat> DATE_FORMAT = ThreadLocal.withInitial(()->new SimpleDateFormat(
            "HH:mm:ss"));
    public static Date addDay(Date date, int days) {
        if (date==null) {
            return null;
        }else{
            Calendar cal = Calendar.getInstance();
            cal.setTime(date);
            cal.add(Calendar.DAY_OF_YEAR,days);
            return cal.getTime();
        }

    }

    public static String getDate8Str(Date date) {
        return YYYYMMDD.get().format(date);
    }

    public static String getTimeAfterDate() {
        return ""+HHMMSS_ONE.get().format(new Date());

    }

    public static Date getDateTimeFormat(String date) {
        try {
            return YYYYMMDD.get().parse(date);
        } catch (ParseException e) {
            log.error(e.getMessage(),e);
        }
        return null;
    }

    public static Date str2Date(String str, SimpleDateFormat sdf) {

        if (null==str||"".equals(str)) {
            return null;
        }

        Date date;
        try {
            date = sdf.parse(str);
            return date;
        } catch (ParseException e) {
            throw new RuntimeException(e);
        }

    }
}
